<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NetworkController
 *
 * @author mateusz.ledzewicz
 */
class NetworkController extends FormListController {
    protected function begin() {
        $this->view = new ViewNetwork();
        
    }
    protected function listRun(){
        $this->view->setSpecialVar('list', Network::getAll());
        $this->view->setModel(Network::loadModel(), 'header'); 
    }
    
    protected function formRun(){
        $id = (isSet($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : null;
        $model = Network::loadModel($id);
        if(!$model->NewRecord)
            $this->view->setVar('disabled', 'disabled');
        else 
            $this->view->setVar('disabled', ''); 
       if(count($_POST) > 0){
            $params = '';
            $model->setValues($_POST);
            
            if($model->NewRecord && Network::loadSpecialModel("name = '{$model->name}' ")->NewRecord == FALSE)
            {
                throw new Exception('Network with this name exists');
            }
            
            
            $structure = explode('-', $model->structure);
            $params .= count($structure).' ';
            foreach($structure as $layer){
                $params .= $layer.' ';    
            }
            
            
            $model->information .= 'steepness_hidden: '.$_POST['steepness_hidden'].' ';
            $model->information .= 'steepness_output:'.$_POST['steepness_output'].' ';
            $model->information .= 'activation_function_hidden:'.$_POST['activation_function_hidden'].' ';
            $model->information .= 'activation_function_output:'.$_POST['activation_function_output'].' ';
            
            $params .= $_POST['steepness_hidden'].' ';
            $params .= $_POST['steepness_output'].' ';
            $params .= $_POST['activation_function_hidden'].' ';
            $params .= $_POST['activation_function_output'].' ';
            $params .= BasicFunctions::prepareFileName($model->name);
            
            exec( ConfigPath::network_create.' '.$params, $output);
            if($output[0] != '1')
            {
                throw new Exception('Create_network failed');
            }
            
            $model->file=$output[1];
            rename($model->file, ConfigPath::network_create_output_dir.$model->file);
            $model->file = ConfigPath::network_create_output_dir.$model->file;
            $model->save();
            header("Location: index.php?step=network");
       
       }
       $this->view->setModel($model, 'edit');
    }


        
        
        //put your code here
}

?>
